//
//  TSHomeTabBarController.m
//  chart
//
//  Created by 曾斌 on 16/4/26.
//  Copyright (c) 2016年 xu. All rights reserved.
//

#import "TSHomeTabBarController.h"
#import "TSHomeViewController.h"
#import "TSHomeMsgController.h"
#import "TSSettingController.h"
#import "TSAssistantController.h"
#import "TSWorkController.h"
#import "TSHomeNewController.h"
#import "TSUserInfoViewController.h"
#import "MMDrawerController.h"
#import "TSAssistantLeftController.h"
@implementation TSHomeTabBarController
{
    UITabBarItem * msgTabItem;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [[NSNotificationCenter defaultCenter] addObserver: self
                                             selector: @selector(setBadgeNumber:)
                                                 name: TSNotification_Badge_Number
                                               object: nil];
    
    TSHomeNewController *homeCol = [[TSHomeNewController alloc] init];
    
    TSHomeMsgController *homeMsgCol = [[TSHomeMsgController alloc] init];
    
    TSBaseNavigationController *nav_vc_msg = [[TSBaseNavigationController alloc] initWithRootViewController:homeMsgCol];
    
    TSWorkController *wordCol = [[TSWorkController alloc] init];
    
    TSAssistantLeftController *leftCol = [[TSAssistantLeftController alloc] init];
    TSAssistantController *assCol = [[TSAssistantController alloc] init];
    
    MMDrawerController *mmCol = [[MMDrawerController alloc] initWithCenterViewController:assCol leftDrawerViewController:leftCol];
    mmCol.title = @"便捷助手";
    mmCol.maximumLeftDrawerWidth = 200;
    [mmCol setOpenDrawerGestureModeMask:MMOpenDrawerGestureModeAll];
    [mmCol setCloseDrawerGestureModeMask:MMCloseDrawerGestureModeAll];
    TSBaseNavigationController *nav_vc_ass = [[TSBaseNavigationController alloc] initWithRootViewController:mmCol];
    
    TSUserInfoViewController *user_vc= [[TSUserInfoViewController alloc] init];
    
    TSBaseNavigationController *nav_vc_set = [[TSBaseNavigationController alloc] initWithRootViewController:user_vc];

    self.viewControllers = @[nav_vc_msg,homeCol,wordCol,nav_vc_ass,nav_vc_set];
    
    NSArray *tabBarTitle = @[@"消息", @"主界面",@"工作",@"便捷助手",@"设置"];
    for (NSUInteger i = 0; i < self.viewControllers.count; i++) {
        UIViewController *viewController = [self.viewControllers objectAtIndex:i];
        if ([viewController isMemberOfClass:[UINavigationController class]]) {
            viewController = [[(UINavigationController *)viewController viewControllers] firstObject];
        }
        
        viewController.tabBarItem = [self tabBarItemWithImage:[NSString stringWithFormat:@"home_nav_nor_%zd", i+1] SelectedImgName:[NSString stringWithFormat:@"home_nav_hlr_%zd", i+1]];
        if(i==0){
            msgTabItem = viewController.tabBarItem;
        }
        
        viewController.tabBarItem.tag = i;
        viewController.tabBarItem.title = tabBarTitle[i];
        [viewController.tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                           RGB(114, 114, 114), UITextAttributeTextColor,
                                                           nil] forState:UIControlStateNormal];
        [viewController.tabBarItem setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                           RGB(13, 156, 255), UITextAttributeTextColor,
                                                           nil] forState:UIControlStateSelected];

    }
    
}

#pragma mark-- tabBarItemImage
- (UITabBarItem *)tabBarItemWithImage:(NSString *)imageName SelectedImgName:(NSString *)selectedImgName {
    UIImage *norImage = [[UIImage imageNamed:imageName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    UIImage *selImage = [[UIImage imageNamed:selectedImgName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:nil image:norImage selectedImage:selImage];

    return item;
}

- (void)setBadgeNumber:(NSNotification *)noti
{
    NSString *count =  noti.object;
    if(![count isEqualToString:@"0"]){
        msgTabItem.badgeValue =count;
    }else{
        msgTabItem.badgeValue = nil;
    }
}

@end
